<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>加载中</title>
    </head>
    <body>
        <p id="tips" style="font-size: 14px;">加载中...</p>
        <script>
            var _hmt = _hmt || [];
            (function() {
                var hm = document.createElement('script');
                hm.src = 'https://hm.baidu.com/hm.js?7d3419dadef3d64f96b9537f7a4e9e87';
                var s = document.getElementsByTagName('script')[0];
                s.parentNode.insertBefore(hm, s);
            })();

            function getQueryString(name) {
                var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)');
                var r = window.location.search.substr(1).match(reg);
                if (r != null) return unescape(r[2]);
                return null;
            }

            function request(options) {
                var host = '/api';
                // var host = 'https://zfx.xmonecode.com/api';
                var request = new XMLHttpRequest();
                request.open(options.method || 'GET', host + options.url);
                request.setRequestHeader('Content-Type', 'application/json');
                request.setRequestHeader('Authorization', localStorage.getItem('token') || '');
                request.onreadystatechange = function() {
                    if (request.readyState === 4 && request.status === 200) {
                        var res = JSON.parse(request.responseText);
                        if (res.code == 1000) {
                            options.success(res.data);
                        }
                    }
                };
                request.send();
            }

            function start() {
                // app类型
                var app = getQueryString('app');
                // sdk类型
                var sdkType = getQueryString('sdkType');
                // token
                var token = getQueryString('token');
                // 0 百亿补贴 1 大转盘
                var scene = getQueryString('scene') || 0;

                // km时 scene交换
                scene = app == 'km' ? (scene == 0 ? 1 : 0) : scene;

                // 返回地址
                var back = getQueryString('back') || '';
                // 渠道
                var channel = getQueryString('channel') || '';

                if (!app || !sdkType) {
                    return (document.getElementById('tips').innerText = '服务异常!!!');
                }

                // 不同app，清空token
                if (app != localStorage.getItem('app')) {
                    localStorage.removeItem('token');
                }

                // 设置app
                localStorage.setItem('app', app);

                function next() {
                    request({
                        url:
                            '/app/comm/domain?scene=' +
                            scene +
                            '&app=' +
                            app +
                            '&sdkType=' +
                            sdkType +
                            '&t=' +
                            Date.parse(new Date()),
                        success: function(res) {
                            var params =
                                '&back=' +
                                encodeURIComponent(back) +
                                '&token=' +
                                localStorage.getItem('token') +
                                '&channel=' +
                                channel;

                            if (scene == 1) {
                                var path = '';

                                if (
                                    res.userInfo.config.newPersonPageType == 1 &&
                                    res.userInfo.user.newPerson == 1 &&
                                    res.userInfo.user.numToday === 0
                                ) {
                                    path = '/pages/index/winners';
                                } else {
                                    path = '/';
                                }
                                location.href = res.url + params + '#' + path;
                            } else {
                                location.href = res.url + params;
                            }
                        }
                    });
                }

                // 地址栏不带token
                if (!token) {
                    // 本地也没有，就生成
                    if (!localStorage.getItem('token')) {
                        request({
                            url: '/app/bwbtUser/getUserToken?app=' + app,
                            success: function(res) {
                                localStorage.setItem('token', res.token);
                                next();
                            }
                        });
                    } else {
                        // 有就用本地
                        next();
                    }
                } else {
                    // 有就保存到本地，并使用
                    localStorage.setItem('token', token);
                    next();
                }
            }

            start();
        </script>
    </body>
</html>
